﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.Base;
using System.Text.RegularExpressions;

namespace ERP_RORZE_PRODUCT.Product.BusOder
{
    public partial class Frm_BusOderAdd : DevExpress.XtraEditors.XtraForm
    {
        ClsBusOderTable busoder = new ClsBusOderTable();
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        public event EventHandler Re_Load;
        DataTable dt;
        public Frm_BusOderAdd()
        {
            InitializeComponent();
        }

        private void Frm_BusOderAdd_Load(object sender, EventArgs e)
        {
            // lấy danh sách PartID truyền vào cbPartID
            dt = clsBusOderDAO.GetPartID();
            DataRow r = dt.NewRow();
            r["PartNo"] = "";
            dt.Rows.Add(r,0);
            cbPartID.DataSource = dt;
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";

            //Lấy danh sách MONo truyền vào cbMONo
            //dt = clsBusOderDAO.GetMONo();
            //cbMONo.DataSource = dt;
            //cbMONo.DisplayMember = "MONo";
            //cbMONo.ValueMember = "MONo";
        }
        void Message(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtBusOderNo.Text == "")
            {
                Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.BOderNo);
            }
            else if (txtBlastQty.Text == "")
            {
                Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.BlastQty);
            }
            //else if (txtChange.Text == "")
            //{
            //    Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.Change);
            //}
            else if (txtHelisertQty.Text == "")
            {
                Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.HelisertQty);
            }
            else if (cbPartID.Text == "")
            {
                MessageBox.Show("Chưa chọn PartID");
            }
            //else if (txtImpordFrom.Text == "")
            //{
            //    Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.ImportFrom);
            //}
            //else if (txtMOQty.Text != "")
            //{
            //    if (IsNumber(txtMOQty.Text) == false)
            //    {
            //        MessageBox.Show("MOQty phải là kiểu số ");
            //    }
            //}
            //else if (txtNote.Text == "")
            //{
            //    Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.Note);
            //}
            else if (txtPlanNo.Text == "")
            {
                Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.planNo);
            }
            else if (txtQty.Text == "")
            {
                Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.Qty);
            }
            else if (txtRawQty.Text == "")
            {
                Message(clsConstantsdb.Entities.ClsDBBusOder.FieldName.RawQty);
            }
            else
            {
                try
                {
                    if (dateChangeDate.EditValue == null)
                    {
                        dateChangeDate.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
                    }

                    busoder.BOderNo = txtBusOderNo.Text;
                    busoder.Date = dateDate.Value;
                    busoder.Dealine = dateDeadLine.Value;
                    busoder.ChangeDate = dateChangeDate.DateTime;
                    busoder.BlastQty = int.Parse(txtBlastQty.Text);
                    busoder.Change = txtChange.Text;
                    busoder.Finished = checkFinished.Checked;
                    busoder.HelisertQty = int.Parse(txtHelisertQty.Text);
                    busoder.Imported = checkImported.Checked;
                    busoder.ImportFrom = txtImpordFrom.Text;
                    //busoder.MONo = cbMONo.Text;
                    //busoder.MOQty = int.Parse(txtMOQty.Text);
                    busoder.Note = txtNote.Text;
                    busoder.PartID = cbPartID.SelectedValue.ToString();
                    busoder.PlanNo = txtPlanNo.Text;
                    busoder.Qty = int.Parse(txtQty.Text);
                    busoder.RawQty = int.Parse(txtRawQty.Text);
                    busoder.Started = checkStarted.Checked;
                    busoder.FinishDate = dateFinishDate.Value;
                    clsBusOderDAO.Insert(busoder);
                    MessageBox.Show("Đã cập nhật thành công");
                    this.Re_Load(sender, e);
                }
                catch
                {
                    MessageBox.Show("các trường RawQty, Qty, MOQty, HelisertQty, BlastQty");
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
    }
}